﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Data.Sql;
using System.Web;
using Microsoft.AspNet.SignalR;
using Bordspil.Models;
using System.Threading;

namespace Bordspil.Hubs
{
    public class GameHub : Hub
    {
        private IAppRepository m_repo = null;

        public GameHub()
        {
            m_repo = new AppRepository();
        }

        public void Join(string groupId)
        {
            var id = Convert.ToInt32(groupId);

            Groups.Add(Context.ConnectionId, groupId);

            var user = (from x in m_repo.GetAllUsers()
                        where x.username == Context.User.Identity.Name
                        select x).ToList();

            var instance = (from c in m_repo.GetAllGames()
                            where c.ID == id
                            select c).ToList();

            if (user[0].ID == instance[0].player1ID)
            {
                user[0].connectID = Context.ConnectionId;
                user[0].playerStatus = 1;
            }
            else
            {
                user[0].connectID = Context.ConnectionId;
                user[0].playerStatus = 2;
                instance[0].player2ID = user[0].ID;
            }
        }

        //Kóði fyrir Samstæðispil
        public void ClickCard(string groupName, string cardId)
        {
            var id = Convert.ToInt32(cardId);

            var cardsFaceUp = (from x in m_repo.GetAllCardsForInstance()
                               where x.Status == 2
                               select x).ToList();

            if(cardsFaceUp.Count < 2)
            {
                var card = (from x in m_repo.GetAllCardsForInstance()
                           where x.DeckOfCardsID == id
                           select x).ToList();

                card[0].Status = 2;

                m_repo.SaveCard(card[0]);
            }

            var cardsFaceUp2nd = (from x in m_repo.GetAllCardsForInstance()
                                  where x.Status == 2
                                  select x).ToList();

            if (cardsFaceUp2nd.Count == 2)
            { 
                var cardID1 = (from x in m_repo.GetAllCards()
                               where x.ID == cardsFaceUp2nd[0].DeckOfCardsID
                               select x).ToList();
                int id1 = cardID1[0].cardID;

                var cardID2 = (from x in m_repo.GetAllCards()
                               where x.ID == cardsFaceUp2nd[1].DeckOfCardsID
                               select x).ToList();
                int id2 = cardID2[0].cardID;

                if (id1 == id2)
                {
                    //Clients.Group(groupName).alertMe(cardId);
                    Clients.Group(groupName).setPoint();

                    cardsFaceUp2nd[0].Status = 1;
                    cardsFaceUp2nd[1].Status = 1;
                    m_repo.SaveCard(cardsFaceUp2nd[0]);
                    m_repo.SaveCard(cardsFaceUp2nd[1]);
                }
                else
                {
                    Thread.Sleep(2000);
                    Clients.Group(groupName).hideCards(cardID1[0].ID, cardID2[0].ID);
                    cardsFaceUp2nd[0].Status = 1;
                    cardsFaceUp2nd[1].Status = 1;
                    m_repo.SaveCard(cardsFaceUp2nd[0]);
                    m_repo.SaveCard(cardsFaceUp2nd[1]);
                }
            }
            Clients.Group(groupName).cardClicked(cardId);

        }

        //Kóði fyrir Ludo
        public void RollDice(string groupName)
        {
            Random number = new Random();
            int value = number.Next(1, 7);
            Clients.Group(groupName).rollDie(value);
        }

        public void MoveFromHome(string groupName, string ped, string cell, string colorID)
        {
                //Sækir rétta mynd fyrir hvert peð
                var img = (from x in m_repo.GetAllImages()
                           where x.nameOfPed == ped
                           select x.imageUrl);

                Clients.Group(groupName).moveFromHome(ped, cell, img);
        }

        //Þetta fall er undirbúningur undir að geta fært peðin um Ludo borðið
        /*public void MoveOnBoard(string groupName, string ped, string cell, string colorID)
        {
            //Sæki mynd peðsins
            var img = (from x in m_repo.GetAllImages()
                       where x.color == colorID
                       select x.imageUrl);

            //Sæki staðsetningu peðsins
            var destination = (from x in m_repo.GetAllImages()
                               where x.nameOfPed == ped
                               select x).ToList();

            destination[0].status = cell;

            Clients.Group(groupName).moveOnBoard(ped, cell, img);
        }*/

        //Kóði fyrir Chat
        public void SendMessage(string group, string message)
        {
            Clients.OthersInGroup(group).sendMessageTo(message);
        }
    }
}